package com.spzx.product.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.core.web.page.TableSupport;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;

import com.spzx.product.domain.*;
import com.spzx.product.domain.vo.IndexVO;
import com.spzx.product.domain.dto.ProductSaveDTO;
import com.spzx.product.domain.vo.ProductSkuDetailsVO;
import com.spzx.product.domain.vo.ProductVO;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Tag(name = "商品管理")
@RequestMapping("product")
public class ProductController extends BaseController {

    @Resource
    ProductService productService;

    @Resource
    ProductSkuService productSkuService;


    @Operation(summary = "内部调用接口：提交订单验证库存")
    @PostMapping("checkSkusStock")
    public R<List<SkuStockDTO>> checkSkusStock(@RequestBody List<SkuStockDTO> skuStockDTOs){

        return R.ok(productSkuService.checkSkusStock(skuStockDTOs));
    }
    @InnerAuth //表示当前接口是内部调用接口  不携带内部调用的头 和正确的值不能访问(aop)
    @Operation(summary = "内部调用接口：提交订单验证价格")
    @PostMapping("checkSkusPrice")
    public R<List<SkuPriceDTO>> checkSkusPrice(@RequestBody List<SkuPriceDTO> skuPriceDTOS){

        return R.ok(productSkuService.checkSkusPrice(skuPriceDTOS));
    }


    @InnerAuth // 表示当前接口必须携带内部调用的头 喝正确的值 否则不能访问 aop负责
    @Operation(summary = "内部调用接口：根据skuid查询购物的商品详情")
    @GetMapping("getSku/{skuId}")
    public R<ProductSku> getSkuById(@PathVariable("skuId") Long skuId){

        return R.ok(productSkuService.getById(skuId));
    }




    @InnerAuth //表示当前接口是内部调用接口  不携带内部调用的头 和正确的值不能访问(aop)
    @Operation(summary = "内部调用接口：根据id查询sku详情")
    @GetMapping("getSkuDetailsVO/{skuId}")
    public R<ProductSkuDetailsVO> getSkuDetailsVO(@PathVariable("skuId")
                                                  Long skuId){
        return R.ok(productService.selectSkuDetailsVOBySkuId(skuId));
    }


    //查询
    @InnerAuth
    @GetMapping("skuList")
    public R<TableDataInfo> skuList(SkuQuery skuQuery){
        //开启若依分页查询
        startPage();
        return R.ok(getDataTable(productService.selectSkulist(skuQuery)));
    }



    @Operation(summary = "商品分页查询")
    @GetMapping("list")
    public TableDataInfo list( Product product){
        //获取分页信息
        Page<ProductVO> page = TableSupport.getPage(ProductVO.class);
        return getDataTable(productService.selectProductList(page,product));
    }


    @Operation(summary = "新增商品数据")
    @PostMapping
    public AjaxResult save(@RequestBody ProductSaveDTO productSaveDTO){

        return toAjax(productService.insertProductInfo(productSaveDTO));
    }

    @Operation(summary = "根据id查询商品详情")
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id){
        return success(productService.selectProductDetailVO(id));
    }

    @Operation(summary = "更新商品数据")
    @PutMapping
    public AjaxResult updateById(@RequestBody ProductSaveDTO productSaveDTO){
        return toAjax(productService.updateProductSaveDTO(productSaveDTO));
    }

    @Operation(summary = "根据id删除商品")
    @DeleteMapping("{ids}")
    public AjaxResult batchDelete(@PathVariable List<Long> ids){
        return toAjax(productService.batchDelete(ids));
    }

    @Operation(summary = "商品审核")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable("id")Long id,
                                        @PathVariable("auditStatus")Integer auditStatus){

        return toAjax(productService.update(new LambdaUpdateWrapper<Product>().eq(Product::getId,id)
                                            .set(Product::getAuditStatus,auditStatus)
                .set(Product::getAuditMessage,auditStatus==1?"审批通过":"审批未通过")
                .set(Product::getUpdateBy, SecurityUtils.getUsername())));
    }

    @Operation(summary = "商品上下架")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable("id")Long id,
                                   @PathVariable("status")Integer status){

        return toAjax(productService.updateStatusByid(id,status));
    }

    //远程调用需要返回结果 使用R
    @InnerAuth //表示当前接口是内部调用接口  不携带内部调用的头 和正确的值不能访问(aop)
    @Operation(summary = "内部调用接口：查询首页热门数据")
    @GetMapping("listIndexVO")
    public R<IndexVO> listIndexVO(){
        return R.ok(productService.selectIndexVO());
    }
}

